package yxy.game.pm2.module.arena.toparena;

import com.alibaba.fastjson.JSON;
import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.battle.Team;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.dfsjingcai;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.battle.BattleType;
import yxy.game.pm2.module.battle.TeamModule;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;
import yxy.game.pm2.module.stat.StatHandle;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 竞技场巅峰赛执行器
 *
 * @ClassName TopAreanHandle
 * @Author jingtao-zheng
 * @Date 2022/9/11 17:03
 * @Version 1.0
 **/
public class TopAreanHandle {
    protected static Logger logger = Logger.create(TopAreanHandle.class);

    /**
     * 参赛等级
     */
    static public enum TopAreanLevel {
        None(0), // 未参赛
        Winner(1), // 冠军
        RunnerUp(2), // 二强
        Four(3), // 四强
        Eight(4), // 八强
        Group(5), // 小组未出线-这个按时间和状态来判断
        GroupWinner(6), // 小组第一
        GroupRunnerUp(7),//小组二强
        GroupFour(8), // 小组四强
        GroupEight(9), // 小组八强
        ;

        private int number;

        private TopAreanLevel(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }
    }


    //比赛code
    static public enum TopAreanCode {
        F12(1), //
        F34(2), //
        F56(3), //
        F78(4), //
        F1256(5), //
        F3478(6), //
        FINAL(7), //
        ;

        private int number;

        private TopAreanCode(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }

        static public TopAreanCode forNumber(int value) {
            switch (value) {
                case 1:
                    return F12;
                case 2:
                    return F34;
                case 3:
                    return F56;
                case 4:
                    return F78;
                case 5:
                    return F1256;
                case 6:
                    return F3478;
                case 7:
                    return FINAL;
            }
            return null;
        }
    }


    //竞技场巅峰赛-活动状态
    static public enum TopAreanState {
        PREPARE(0),  //巅峰赛未开始
        GROUP(1), //巅峰小组赛准备阶段
        QUIZ64(2),   //巅峰小组赛64强竞猜
        QUIZ32(3),   //巅峰小组赛32强竞猜
        QUIZ16(4),   //巅峰小组赛16强竞猜
        OFFSEASONGROUP(5),//小组赛结束展示
        APEREADY(6), //冠军赛准备阶段
        QUIZ8(7),     //巅峰冠军赛8强竞猜
        QUIZ4(8),     //巅峰冠军赛4强竞猜
        QUIZ2(9),     //巅峰冠军赛决赛竞猜
        OFFSEASON(10) //赛季结束展示
        ;

        private int number;

        TopAreanState(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }
    }

    //竞技场巅峰赛-时间表
    static public enum TopAreanEnum {
        // 休赛日
        STEP_PREPARE(TopAreanState.PREPARE), //巅峰赛未开始
        //小组赛阶段
        STEP_GROUP(TopAreanState.GROUP), //巅峰小组赛准备中
        STEP_QUIZ64(TopAreanState.QUIZ64), //巅峰小组赛64强竞猜中
        STEP_QUIZ32(TopAreanState.QUIZ32), //巅峰小组赛32强竞猜中
        STEP_QUIZ16(TopAreanState.QUIZ16), //巅峰小组赛16强竞猜中
        STEP_OFFSEASONGROUP(TopAreanState.OFFSEASONGROUP), //巅峰小组赛结束展示
        //冠军赛阶段
        STEP_APEREADY(TopAreanState.APEREADY), //巅峰冠军赛准备中
        STEP_QUIZ8(TopAreanState.QUIZ8), //巅峰冠军赛8强竞猜
        STEP_QUIZ4(TopAreanState.QUIZ4), //巅峰冠军赛4强竞猜
        STEP_QUIZ2(TopAreanState.QUIZ2), //巅峰冠军赛决赛竞猜中
        STEP_OFFSEASON(TopAreanState.OFFSEASON), //赛季结束展示
        ;

        private TopAreanState state;

        private TopAreanEnum(TopAreanState state) {
            this.state = state;
        }

        public TopAreanState getState() {
            return state;
        }

        //获取下一个阶段
        public TopAreanEnum getNext() {
            switch (this) {
                case STEP_PREPARE:
                    return STEP_GROUP;
                case STEP_GROUP:
                    return STEP_QUIZ64;
                case STEP_QUIZ64:
                    return STEP_QUIZ32;
                case STEP_QUIZ32:
                    return STEP_QUIZ16;
                case STEP_QUIZ16:
                    return STEP_OFFSEASONGROUP;
                case STEP_OFFSEASONGROUP:
                    return STEP_APEREADY;
                case STEP_APEREADY:
                    return STEP_QUIZ8;
                case STEP_QUIZ8:
                    return STEP_QUIZ4;
                case STEP_QUIZ4:
                    return STEP_QUIZ2;
                case STEP_QUIZ2:
                    return STEP_OFFSEASON;
                default:
                    return STEP_PREPARE;
            }
        }
    }


    public BattleModule handleBattle(String playerCode1, String playerCode2) {
        BattleHandle battleHandle = new BattleHandle();

        BattleModule battleModule = BattleModule.create(playerCode1);
        battleModule.setType(BattleType.TOPAREAN);
        battleModule.setCode(BattleType.TOPAREAN.name());

        // 玩家的team
        TeamModule teamA = battleHandle.teamPlayer(playerCode1, Format.FormatType.普通);
        battleModule.setTeamA(teamA);

        // 玩家的team
        TeamModule teamB = battleHandle.teamPlayer(playerCode2, Format.FormatType.普通);
        battleModule.setTeamB(teamB);

        Team team_a = teamA.team(true);
        Team team_b = teamB.team(false);
        team_a.setTeam对手(team_b);
        team_b.setTeam对手(team_a);

        // 处理战斗
        battleHandle.battleExecute(battleModule, team_a, team_b, 20);
        return battleModule;
    }

    /**
     * 巅峰赛合区操作
     *
     * @param areas 合并的区
     */
    public void mregeAreaOperate(Collection<AreaBean> areas) {
        List<String> allQuiz_64 = new ArrayList<>();
        List<String> allQuiz_32 = new ArrayList<>();
        List<String> allQuiz_16 = new ArrayList<>();
        List<String> allQuiz_8 = new ArrayList<>();
        List<String> allQuiz_4 = new ArrayList<>();
        List<String> allQuiz_2 = new ArrayList<>();
        for (AreaBean area : areas) {
            //查看比赛状态-只有在竞猜时间段的区才会补偿竞猜奖励
            TopAreanModule topAreanModule = TopAreanModule.get(area.getAreaId());
            try {
                if (topAreanModule.tryLock()) {
                    int number = topAreanModule.getState().getState().getNumber();
                    if (number == 2) {
                        //1.获取参与(小组赛和决赛)8强竞猜的玩家code
                        List<String> quiz_64 = getQuiz_64(topAreanModule);
                        allQuiz_64.addAll(quiz_64);
                    } else if (number == 3) {
                        //1.获取参与(小组赛和决赛)8强竞猜的玩家code
                        List<String> quiz_32 = getQuiz_32(topAreanModule);
                        allQuiz_32.addAll(quiz_32);
                    } else if (number == 4) {
                        //1.获取参与(小组赛和决赛)8强竞猜的玩家code
                        List<String> quiz_16 = getQuiz_16(topAreanModule);
                        allQuiz_16.addAll(quiz_16);
                    } else if (number == 7) {
                        //1.获取参与(小组赛和决赛)8强竞猜的玩家code
                        List<String> quiz_8 = getQuiz_8(topAreanModule);
                        allQuiz_8.addAll(quiz_8);
                    } else if (number == 8) {
                        //2.获取参与(小组赛和决赛)4强竞猜的玩家code
                        List<String> quiz_4 = getQuiz_4(topAreanModule);
                        allQuiz_4.addAll(quiz_4);
                    } else if (number == 9) {
                        //3.获取参与(小组赛和决赛)决赛竞猜的玩家code
                        List<String> quiz_2 = getQuiz_2(topAreanModule);
                        allQuiz_2.addAll(quiz_2);
                    }
                    //重置活动
                    topAreanModule.resetState(DateTime.now().withTimeAtStartOfDay());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                topAreanModule.unlock();
            }
        }
        logger.info("竞技场巅峰赛%s合服补偿玩家:参与8强竞猜玩家:%s,参与4强竞猜玩家:%s,参与2强竞猜玩家:%s,参与64强竞猜玩家:%s,参与32强竞猜玩家:%s,参与16强竞猜玩家:%s",
                JSON.toJSONString(areas),
                JSON.toJSONString(allQuiz_8),
                JSON.toJSONString(allQuiz_4),
                JSON.toJSONString(allQuiz_2),
                JSON.toJSONString(allQuiz_64),
                JSON.toJSONString(allQuiz_32),
                JSON.toJSONString(allQuiz_16)
        );
        String title = GameString.竞技场巅峰赛合服补偿_titl().getText();
        String content = GameString.竞技场巅峰赛合服补偿_msg().getText();
        Map<String, dfsjingcai> allData = DataCenter.getAllData(dfsjingcai.class);
        Collection<dfsjingcai> dataValues = allData.values();
        dfsjingcai data_8 = dataValues.stream().filter(v -> v.getType() == 3).findFirst().get();
        Currencies rewards_8 = new Currencies(Currency.create(2, -1, data_8.getCostnum()));
        for (String playerCode_8 : allQuiz_8) {
            new MailHandle().sendMail(playerCode_8, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_8);
        }
        dfsjingcai data_4 = dataValues.stream().filter(v -> v.getType() == 2).findFirst().get();
        Currencies rewards_4 = new Currencies(Currency.create(2, -1, data_4.getCostnum()));
        for (String playerCode_8 : allQuiz_4) {
            new MailHandle().sendMail(playerCode_8, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_4);
        }
        dfsjingcai data_2 = dataValues.stream().filter(v -> v.getType() == 1).findFirst().get();
        Currencies rewards_2 = new Currencies(Currency.create(2, -1, data_2.getCostnum()));
        for (String playerCode_8 : allQuiz_2) {
            new MailHandle().sendMail(playerCode_8, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_2);
        }


        dfsjingcai data_64 = dataValues.stream().filter(v -> v.getType() == 6).findFirst().get();
        Currencies rewards_64 = new Currencies(Currency.create(2, -1, data_64.getCostnum()));
        for (String playerCode_64 : allQuiz_64) {
            new MailHandle().sendMail(playerCode_64, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_64);
        }
        dfsjingcai data_32 = dataValues.stream().filter(v -> v.getType() == 5).findFirst().get();
        Currencies rewards_32 = new Currencies(Currency.create(2, -1, data_32.getCostnum()));
        for (String playerCode_32 : allQuiz_32) {
            new MailHandle().sendMail(playerCode_32, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_32);
        }
        dfsjingcai data_16 = dataValues.stream().filter(v -> v.getType() == 4).findFirst().get();
        Currencies rewards_16 = new Currencies(Currency.create(2, -1, data_16.getCostnum()));
        for (String playerCode_16 : allQuiz_16) {
            new MailHandle().sendMail(playerCode_16, MailTypeEnum.Mail, title, content, CCReason.巅峰赛, rewards_16);
        }
    }

    private List<String> getQuiz_64(TopAreanModule topAreanModule) {
        List<String> quiz_64 = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(i);
            TopAreanStructure topAreanStructure12 = structureHashMap.get(TopAreanCode.F12);
            TopAreanStructure topAreanStructure34 = structureHashMap.get(TopAreanCode.F34);
            TopAreanStructure topAreanStructure56 = structureHashMap.get(TopAreanCode.F56);
            TopAreanStructure topAreanStructure78 = structureHashMap.get(TopAreanCode.F78);
            if (topAreanStructure12 == null || topAreanStructure34 == null || topAreanStructure56 == null || topAreanStructure78 == null) {
                continue;
            }
            quiz_64.addAll(topAreanStructure12.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure12.getPlayer2().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure34.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure34.getPlayer2().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure56.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure56.getPlayer2().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure78.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_64.addAll(topAreanStructure78.getPlayer2().getFans().stream().collect(Collectors.toList()));
        }
        return quiz_64;
    }

    private List<String> getQuiz_32(TopAreanModule topAreanModule) {
        List<String> quiz_32 = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(i);
            TopAreanStructure topAreanStructure1256 = structureHashMap.get(TopAreanCode.F1256);
            TopAreanStructure topAreanStructure3478 = structureHashMap.get(TopAreanCode.F3478);
            if (topAreanStructure1256 == null || topAreanStructure3478 == null) {
                continue;
            }
            quiz_32.addAll(topAreanStructure1256.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_32.addAll(topAreanStructure1256.getPlayer2().getFans().stream().collect(Collectors.toList()));
            quiz_32.addAll(topAreanStructure3478.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_32.addAll(topAreanStructure3478.getPlayer2().getFans().stream().collect(Collectors.toList()));
        }
        return quiz_32;
    }

    private List<String> getQuiz_16(TopAreanModule topAreanModule) {
        List<String> quiz_16 = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(i);
            TopAreanStructure topAreanStructureFINAL = structureHashMap.get(TopAreanCode.FINAL);
            if (topAreanStructureFINAL == null) {
                continue;
            }
            quiz_16.addAll(topAreanStructureFINAL.getPlayer1().getFans().stream().collect(Collectors.toList()));
            quiz_16.addAll(topAreanStructureFINAL.getPlayer2().getFans().stream().collect(Collectors.toList()));
        }
        return quiz_16;
    }


    private List<String> getQuiz_8(TopAreanModule topAreanModule) {
        List<String> quiz_8 = new ArrayList<>();
        HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(9);
        TopAreanStructure topAreanStructure12 = structureHashMap.get(TopAreanCode.F12);
        TopAreanStructure topAreanStructure34 = structureHashMap.get(TopAreanCode.F34);
        TopAreanStructure topAreanStructure56 = structureHashMap.get(TopAreanCode.F56);
        TopAreanStructure topAreanStructure78 = structureHashMap.get(TopAreanCode.F78);
        if (topAreanStructure12 == null || topAreanStructure34 == null || topAreanStructure56 == null || topAreanStructure78 == null) {
            return quiz_8;
        }
        quiz_8.addAll(topAreanStructure12.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure12.getPlayer2().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure34.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure34.getPlayer2().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure56.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure56.getPlayer2().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure78.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_8.addAll(topAreanStructure78.getPlayer2().getFans().stream().collect(Collectors.toList()));
        return quiz_8;
    }

    private List<String> getQuiz_4(TopAreanModule topAreanModule) {
        List<String> quiz_4 = new ArrayList<>();
        HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(9);
        TopAreanStructure topAreanStructure1256 = structureHashMap.get(TopAreanCode.F1256);
        TopAreanStructure topAreanStructure3478 = structureHashMap.get(TopAreanCode.F3478);
        if (topAreanStructure1256 == null || topAreanStructure3478 == null) {
            return quiz_4;
        }
        quiz_4.addAll(topAreanStructure1256.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_4.addAll(topAreanStructure1256.getPlayer2().getFans().stream().collect(Collectors.toList()));
        quiz_4.addAll(topAreanStructure3478.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_4.addAll(topAreanStructure3478.getPlayer2().getFans().stream().collect(Collectors.toList()));
        return quiz_4;
    }

    private List<String> getQuiz_2(TopAreanModule topAreanModule) {
        List<String> quiz_2 = new ArrayList<>();
        HashMap<TopAreanCode, TopAreanStructure> structureHashMap = topAreanModule.matchGroup(9);
        TopAreanStructure topAreanStructureFINAL = structureHashMap.get(TopAreanCode.FINAL);
        if (topAreanStructureFINAL == null) {
            return quiz_2;
        }
        quiz_2.addAll(topAreanStructureFINAL.getPlayer1().getFans().stream().collect(Collectors.toList()));
        quiz_2.addAll(topAreanStructureFINAL.getPlayer2().getFans().stream().collect(Collectors.toList()));
        return quiz_2;
    }

}
